package com.NyCloud.controller;


import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.NyCloud.entity.Location;
import com.NyCloud.entity.User;
import com.NyCloud.service.ILocationService;
import com.NyCloud.service.IUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 作者
 * @since 2024-07-26
 */
@RestController
@RequestMapping("/location")
@Api(tags = "地理位置")
@CrossOrigin
public class LocationController {

    @Autowired
    private ILocationService locationService;

    @Autowired
    private IUserService userService;

    @ApiOperation("添加位置")
    @PostMapping
    public SaResult updateLocation(@RequestBody Location location) {
        Location serviceOne = locationService.getOne(new QueryWrapper<Location>()
                .eq("user_id", location.getUserId()));
        serviceOne.setRemark(location.getRemark());
        serviceOne.setLat(location.getLat());
        serviceOne.setLng(location.getLng());
        locationService.updateById(serviceOne);
        return SaResult.ok();
    }

    @ApiOperation("获取位置")
    @GetMapping
    public SaResult getLocation() {
        Location serviceOne = locationService.getOne(new QueryWrapper<Location>()
                .eq("user_id", StpUtil.getLoginIdAsInt()));
        return SaResult.data(serviceOne);
    }

}
